package com.demo12;

import java.util.Comparator;
import java.util.HashSet;
import java.util.TreeSet;

public class TreeSetDemo3 {
    public static void main(String[] args) {
        //实例化信息
        TreeSet<Truck> treeSet = new TreeSet<Truck>(new Comparator<Truck>() {
            @Override
            public int compare(Truck t1, Truck t2) {
                //强制类型转换
                int t = (int)(t1.getWeight() - t2.getWeight());
                int tx = t == 0 ? t1.getBrand().compareTo(t2.getBrand()) : t;
                return tx;
            }
        });

        //创建实例化对象
        Truck t1 = new  Truck("FuTian",12.0);
        Truck t2 = new Truck("Ford", 16.0);
        Truck t3 = new Truck("Bench", 14.0);
        Truck t4 = new Truck("Kaman", 12.0);

        treeSet.add(t1);
        treeSet.add(t2);
        treeSet.add(t3);
        treeSet.add(t4);
        for (Truck t: treeSet){
            System.out.println(t.getBrand()+"-----"+t.getWeight()+"T");
        }
    }
}
